package com.dangz.dans.controller;

import com.dangz.dans.entity.CommonReqEntity;
import com.dangz.dans.entity.CommonResponse;
import com.dangz.dans.model.User;
import com.dangz.dans.model.req.UserPageReq;
import com.dangz.dans.service.UserServiceImpl;
import com.dangz.dans.response.ApiEnum;
import com.dangz.dans.response.CodeEnum;
import com.dangz.dans.response.TypeEnum;
import com.dangz.dans.utils.PageUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * UserController 用户管理基本操作
 * @author dangz
 * @since 2017/12/18 16:10
 */
@RestController
@RequestMapping("dans/comUser")
@Api(tags = "用户基本操作")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UserServiceImpl userService;

    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", value = "Authorization", name = HttpHeaders.AUTHORIZATION, dataType = "String")
    })
    @PostMapping(value = "listPage")
    public CommonResponse listPage(@RequestBody CommonReqEntity<UserPageReq> req){
        CommonResponse commonResponse = null;
        try {
            PageUtil<User> user = userService.listPage(req.getCurPage(), req.getPageSize(), req.getRequestEntity().getUserName());
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                    ApiEnum.XTGL, CodeEnum.SUCCESS_SERVICE_LOCAL), CodeEnum.SUCCESS_SERVICE_LOCAL.getInfo(), user);
        } catch (Exception e) {
            e.printStackTrace();
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Error,
                    ApiEnum.XTGL, CodeEnum.FAIL_SERVICE_LOCAL), CodeEnum.FAIL_SERVICE_LOCAL.getInfo());
        }
        return commonResponse;
    }

    @ApiOperation("查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", value = "Authorization", name = HttpHeaders.AUTHORIZATION, dataType = "String")
    })
    @GetMapping(value = "findOne")
    public CommonResponse findOne(@ApiParam("用户ID") @RequestParam String userId){
        if (StringUtils.isEmpty(userId)) {
            return error("请传入用户ID");
        }
        CommonResponse commonResponse = null;
        try {
//            User user = userService.findOne(userId);
            User user = userService.findOne(userId);
            logger.info("User:{}", user);
            List<?> list = userService.findOneSql(userId);
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                    ApiEnum.XTGL, CodeEnum.SUCCESS_SERVICE_LOCAL), CodeEnum.SUCCESS_SERVICE_LOCAL.getInfo(), list);
        } catch (Exception e) {
            e.printStackTrace();
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Error,
                    ApiEnum.XTGL, CodeEnum.FAIL_SERVICE_LOCAL), CodeEnum.FAIL_SERVICE_LOCAL.getInfo());
        }
        return commonResponse;
    }

    /**
     * 返回错误提示
     * @param message
     * @return
     */
    private CommonResponse error(String message){
        return new CommonResponse<>(TypeEnum.getFullRespCode(TypeEnum.Error, ApiEnum.XTGL, CodeEnum.SUCCESS_NO_DATA_THIRD), message);
    }
}
